package com.trdcore.zyzkt.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.trdcore.zyzkt.model.vod.Teacher;
import com.trdcore.zyzkt.resp.ResultData;
import com.trdcore.zyzkt.resp.ReturnCodeEnum;
import com.trdcore.zyzkt.service.TeacherService;
import com.trdcore.zyzkt.vo.vod.TeacherQueryVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Luffy
 * @since 2024-03-15
 */
@RestController
@Slf4j
@RequestMapping("/admin/vod/teacher")
@Tag(name = "教师微服务模块",description = "教师管理")

public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    //查询所有讲师
    @GetMapping("/findAll")
    @Operation(summary = "查询所有",description = "查询所有讲师")
    public ResultData<List<Teacher>> findAllTeacher(){
        List<Teacher> list = teacherService.list();
        return ResultData.success(list);
    }

    //逻辑删除讲师
    @DeleteMapping("/remove/{id}")
    @Operation(summary = "删除教师",description = "删除指定教师")
    public ResultData removeTeacher(@PathVariable("id") String id){
        boolean isSuccess = teacherService.removeById(id);
        if(isSuccess){
            return ResultData.success(null);
        }

        return ResultData.fail(ReturnCodeEnum.RC999.getCode(),"删除失败");
    }

    //条件查询分页
    @PostMapping("/findQueryPage/{current}/{limit}")
    @Operation(summary = "条件查询分页",description = "条件查询分页")
    public ResultData findPage(@PathVariable("current")long current, @PathVariable("limit")long limit, TeacherQueryVo teacherQueryVo){
        //判断teacherQueryVo是否为空
        Page<Teacher> pageParam=new Page<>(current,limit);
        if(null == teacherQueryVo){
            IPage<Teacher> pageModel=teacherService.page(pageParam,null);
            return ResultData.success(pageModel);
        }
        else {
            //获取条件值,进行非空判断
            String name= teacherQueryVo.getName();
            Integer level= teacherQueryVo.getLevel();
            String joinDateBegin= teacherQueryVo.getJoinDateBegin();
            String joinDateEnd= teacherQueryVo.getJoinDateEnd();
            //非空判断
            QueryWrapper<Teacher> wrapper=new QueryWrapper<>();
            if(!StringUtils.isEmpty(name)){
                wrapper.like("name",name);
            }
            if(!(level == null)){
                wrapper.eq("level",level);
            }
            if(!StringUtils.isEmpty(joinDateBegin)){
                wrapper.ge("join_date",joinDateBegin);
            }
            if(!StringUtils.isEmpty(joinDateEnd)){
                wrapper.le("join_date",joinDateEnd);
            }
            //分页查询
            IPage<Teacher> pageModel = teacherService.page(pageParam, wrapper);
            return ResultData.success(pageModel);
        }
    }

    @PostMapping("/saveTeacher")
    @Operation(summary = "添加教师",description = "添加教师")
    public ResultData saveTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.save(teacher);
        if(isSuccess){
            return ResultData.success(null);
        }
        return ResultData.fail(ReturnCodeEnum.RC999.getCode(), "添加教师失败");
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据id查询教师信息",description = "根据id查询教师信息")
    public ResultData getTeacherById(@PathVariable("id") Long id){

        Teacher teacher=teacherService.getById(id);
        return ResultData.success(teacher);
    }

    @PutMapping("/update")
    @Operation(summary = "修改教师信息",description = "修改教师信息")
    public ResultData updateTeacher(@RequestBody Teacher teacher){
        boolean isSuccess = teacherService.updateById(teacher);
        if(isSuccess){
            return ResultData.success(null);
        }
        return ResultData.fail(ReturnCodeEnum.RC999.getCode(), "修改失败");
    }

    //批量删除讲师
    @DeleteMapping("/removeBatch")
    @Operation(summary = "批量删除讲师",description = "批量删除讲师")
    public ResultData removeBatch(@RequestBody List<Long> idList){
        boolean isSuccess = teacherService.removeBatchByIds(idList);
        if(isSuccess){
            return ResultData.success(null);
        }
        return ResultData.fail(ReturnCodeEnum.RC999.getCode(), "批量删除失败");
    }

    @Operation(summary = "根据id查询",description = "根据id查询")
    @GetMapping("inner/getTeacher/{id}")
    public Teacher getTeacherInfo(@PathVariable("id") Long id) {
        Teacher teacher = teacherService.getById(id);
        return teacher;
    }

}

